#!/bin/bash
# parameter 1: sql command
# parameter 2: port
# parameter 3: password
# parameter 4(optional): charset

set -eu

TIDB_PORT=4000
user="root"
OUTFILE="$TEST_DIR/sql_res.$TEST_NAME.txt"
echo "[$(date)] Executing SQL: $1" >$OUTFILE

if [[ "$2" = $TIDB_PORT ]]; then
	user="test"
fi

if [[ $# -ge 4 ]]; then
	echo "$1" | iconv -f utf8 -t $4 | mysql -u$user -h127.0.0.1 -P$2 -p$3 --default-character-set $4 -E >>$OUTFILE
else
	mysql -u$user -h127.0.0.1 -P$2 -p$3 --default-character-set utf8 -E -e "$1" >>$OUTFILE
fi
